﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// ItemSettingView.xaml 的交互逻辑
    /// </summary>
    public partial class ItemSettingView : Window
    {
        public ItemSettingView()
        {
            InitializeComponent();
            Load();
        }
        void Load()
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings.AllKeys.Contains("ItemNo"))
                {
                    txtItemNo.Text = config.AppSettings.Settings["ItemNo"].Value;
                }
                if (config.AppSettings.Settings.AllKeys.Contains("ItemBatchNo"))
                {
                    txtItemBatchNo.Text = config.AppSettings.Settings["ItemBatchNo"].Value;
                }
                if (config.AppSettings.Settings.AllKeys.Contains("ItemNum"))
                {
                    txtItemNum.Text = config.AppSettings.Settings["ItemNum"].Value;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载数据发生错", "错误");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    if (config.AppSettings.Settings.AllKeys.Contains("ItemNo"))
                    {
                        config.AppSettings.Settings["ItemNo"].Value = txtItemNo.Text.Trim();
                    }
                    else
                    {
                        config.AppSettings.Settings.Add("ItemNo", txtItemNo.Text.Trim());
                    }
                    if (config.AppSettings.Settings.AllKeys.Contains("ItemBatchNo"))
                    {
                        config.AppSettings.Settings["ItemBatchNo"].Value = txtItemBatchNo.Text.Trim();
                    }
                    else
                    {
                        config.AppSettings.Settings.Add("ItemBatchNo", txtItemBatchNo.Text.Trim());
                    }
                    if (config.AppSettings.Settings.AllKeys.Contains("ItemNum"))
                    {
                        config.AppSettings.Settings["ItemNum"].Value = txtItemNum.Text.Trim();
                    }
                    else
                    {
                        config.AppSettings.Settings.Add("ItemNum", txtItemNum.Text.Trim());
                    }



                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");

                    GlobalValues.ItemNo = txtItemNo.Text.Trim();
                    GlobalValues.ItemBatchNo = txtItemBatchNo.Text.Trim();
                    GlobalValues.ItemNum = txtItemNum.Text.Trim();

                    Messenger.Default.Send<string>("", "CmdUpdateItemNo");
                    MessageBox.Show("录入成功 ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("录入失败 {0}", ex.Message), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

        }




    }
}
